

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.dao.DiscoverDao;
import com.yz.dao.DiscoverRecordDao;
import com.yz.dao.SurveyRecordDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("discoverRecordService")
public class DiscoverRecordServiceImpl extends ServiceImpl<DiscoverRecordDao,DiscoverRecordEntity> implements DiscoverRecordService {
    @Autowired
    CheckService checkService;
    @Autowired
    ProjectService projectService;
@Autowired
    RecordDetailNoformService recordDetailNoformService;
    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postSpecimen(DiscoverRecordForm t) {
        DiscoverRecordEntity data = t.getDiscoverEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(data.getCreateAt());
        check.setCreateUser(data.getCreateUser());
        check.setName(projectService.selectById(data.getProjectId()).getName());
        check.setNameForm(contant.record_name[data.getType()-1]);
        check.setProjectId(data.getProjectId());
        check.setStatus(1);
        check.setType(6);
        check.setTypeForm(2);
        if(data.getId()==0){
            this.baseMapper.insert(data);
            long id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[5]+data.getType()+id);
            checkService.insert(check);
            List<RecordDetailDiscoverEntity> collections = t.getConnectionsEntities();
            for (int i = 0; i < collections.size(); i++) {
                RecordDetailDiscoverEntity item = collections.get(i);
                item.setRecordId(id);
                recordDetailNoformService.insert(item);
            }
        }else{
            long id = data.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[5]+data.getType()+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[5]+data.getType()+id));
            this.baseMapper.update(data,new EntityWrapper<DiscoverRecordEntity>().eq("id",id));
            recordDetailNoformService.delete(new EntityWrapper<RecordDetailDiscoverEntity>().eq("record_id",id));
            List<RecordDetailDiscoverEntity> collections = t.getConnectionsEntities();
            for (int i = 0; i < collections.size(); i++) {
                RecordDetailDiscoverEntity item = collections.get(i);
                item.setRecordId(id);
                recordDetailNoformService.insert(item);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public DiscoverRecordForm getSpecimenDetail(long id) {
        DiscoverRecordForm data = new DiscoverRecordForm();
        DiscoverRecordEntity t = this.baseMapper.selectById(id);
        List<RecordDetailDiscoverEntity> collections = recordDetailNoformService.selectList(new EntityWrapper<RecordDetailDiscoverEntity>().eq("record_id",id));
        data.setDiscoverEntity(t);
        data.setConnectionsEntities(collections);
        return data;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteSpecimen(long id) {
        this.deleteById(id);
        checkService.deleteMe(6,2,id);
        recordDetailNoformService.delete(new EntityWrapper<RecordDetailDiscoverEntity>().eq("record_id",id));
    }
}
